﻿using KELT.KELTObjects.ViewportObjectModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;

namespace KELT.Animation.AnimationEffect
{

    //인덱스보류
    public class BounceAnimation : IAnimation
    {
        private DoubleAnimation[] bounceAniY;
        private DoubleAnimation[] bounceAniX;
        private DoubleAnimation[] bounceAniZ;
        private int count = 7;

        public BounceAnimation()
        {
            bounceAniX = new DoubleAnimation[count];
            bounceAniY = new DoubleAnimation[count];
            bounceAniZ = new DoubleAnimation[count];

            for (int i = 0; i < count; i++)
            {
                bounceAniY[i] = new DoubleAnimation();

                bounceAniX[i] = new DoubleAnimation();
            }
        }

        public override void SetAnimation(Storyboard myStoryboard, ViewportObj obj)
        {
            animationName = "바운스";

            double height = 80;
            double time = (endTime - startTime) / count;


            for (int i = 0; i < count; i++)
            {
                bounceAniY[i].Duration = new Duration(TimeSpan.FromSeconds(time));
                bounceAniY[i].BeginTime = TimeSpan.FromSeconds(startTime + (time * i));
                if (i % 2 == 0)
                {
                    bounceAniY[i].AccelerationRatio = 1;
                    bounceAniY[i].From = obj.getPosition().Y + height;
                    bounceAniY[i].To = obj.getPosition().Y;

                }
                else
                {
                    height /= 2;
                    bounceAniY[i].DecelerationRatio = 1;
                    bounceAniY[i].From = obj.getPosition().Y;
                    bounceAniY[i].To = obj.getPosition().Y + height;
                }
                setBounce(myStoryboard, obj, bounceAniY[i]);

                bounceAniX[i].Duration = new Duration(TimeSpan.FromSeconds(time * count));
                bounceAniX[i].BeginTime = TimeSpan.FromSeconds(startTime);
                bounceAniX[i].From = obj.getPosition().X - 30;
                bounceAniX[i].To = obj.getPosition().X;

                Storyboard.SetTargetProperty(bounceAniX[i], new PropertyPath(TranslateTransform3D.OffsetXProperty));
                Storyboard.SetTargetName(bounceAniX[i], AnimationInfo.ANIMATION_TRANSLATE + obj.getIndexNumber());
                myStoryboard.Children.Add(bounceAniX[i]);



                bounceAniZ[i] = new DoubleAnimation();
                bounceAniZ[i].Duration = new Duration(TimeSpan.FromSeconds(time * count));
                bounceAniZ[i].BeginTime = TimeSpan.FromSeconds(startTime);
                bounceAniZ[i].From = obj.getPosition().Z - 100;
                bounceAniZ[i].To = obj.getPosition().Z;

                Storyboard.SetTargetProperty(bounceAniZ[i], new PropertyPath(TranslateTransform3D.OffsetZProperty));
                Storyboard.SetTargetName(bounceAniZ[i], AnimationInfo.ANIMATION_TRANSLATE + obj.getIndexNumber());
                myStoryboard.Children.Add(bounceAniZ[i]);


            }

        }



        public override void changeAnimation(Storyboard myStoryboard)
        {

            for (int i = 0; i < count; i++)
            {
                myStoryboard.Children.Remove(bounceAniX[i]);
                myStoryboard.Children.Remove(bounceAniY[i]);
            }
            double time = (endTime - startTime) / count;

            for (int i = 0; i < count; i++)
            {

                bounceAniY[i].Duration = new Duration(TimeSpan.FromSeconds(time));
                bounceAniY[i].BeginTime = TimeSpan.FromSeconds(startTime + (time * i));

                myStoryboard.Children.Add(bounceAniY[i]);
                bounceAniX[i].Duration = new Duration(TimeSpan.FromSeconds(time * count));
                bounceAniX[i].BeginTime = TimeSpan.FromSeconds(startTime);
                myStoryboard.Children.Add(bounceAniX[i]);

            }

        }

        public void setBounce(Storyboard myStoryboard, ViewportObj obj, DoubleAnimation bounceAniY)
        {
            Storyboard.SetTargetProperty(bounceAniY, new PropertyPath(TranslateTransform3D.OffsetYProperty));
            Storyboard.SetTargetName(bounceAniY, AnimationInfo.ANIMATION_TRANSLATE + obj.getIndexNumber());
            myStoryboard.Children.Add(bounceAniY);
        }

        public override void deleteAnimation(Storyboard myStoryboard)
        {
            for (int i = 0; i < count; i++)
            {
                myStoryboard.Children.Remove(bounceAniX[i]);
                myStoryboard.Children.Remove(bounceAniY[i]);
            }
        }

    }
}
